package com.wc.alorithm_luogu.P1104;

import java.util.LinkedList;
import java.util.Scanner;

/**
 * @Author congge
 * @Date 2023/5/13 16:50
 * @description https://www.luogu.com.cn/problem/P1104
 * 生日
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        LinkedList<People> peoples = new LinkedList<>();

        for (int i = 0; i < n; i++) {
            People people = new People();
            people.name = sc.next();
            people.year = Integer.parseInt(sc.next());
            people.month = Integer.parseInt(sc.next());
            people.day = Integer.parseInt(sc.next());
            // 如果people的生日<=当前位置,插入
            for (int j = 0; j < i; j++) {
                if (compareBsd(people, peoples.get(j))) {
                    peoples.add(j, people);
                    break;
                }
            }

            // 如果都没有插入,说明是最后一个
            if (peoples.size() == i) {
                peoples.add(people);
            }
        }


        for (int i = 0; i < n; i++) {
            System.out.println(peoples.get(i).name);
        }
    }

    // 比较生日
    static boolean compareBsd(People people1, People people2) {
        if (people1.year == people2.year) {
            if (people1.month == people2.month) {
                if (people1.day == people2.day) {
                    return true;
                }
                return people1.day < people2.day;
            }
            return people1.month < people2.month;
        }

        return people1.year < people2.year;
    }

    static class People {
        String name;

        int year;
        int month;
        int day;
    }
}
